﻿
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;

namespace StarForce
{
    public abstract class UGuiForm : UIFormLogic
    {
        public const int DepthFactor = 10;
        
        private static Font s_MainFont = null;
        private Canvas m_CachedCanvas = null;
        private CanvasGroup m_CanvasGroup = null;

        /// <summary>
        /// 原始深度
        /// </summary>
        public int OriginalDepth { get; private set; }

        /// <summary>
        /// 深度
        /// </summary>
        public int Depth
        {
            get { return m_CachedCanvas.sortingOrder; }
        }

        /// <summary>
        /// 关闭界面
        /// </summary>
        public void Close()
        {
            GFGame.UI.CloseUIForm(this);
        }


        /// <summary>
        /// 播放UI音效
        /// </summary>
        // public void PlayUISound(UISoundId uiSoundId)
        // {
        //     GameEntry.Sound.PlayUISound((int)uiSoundId);
        // }
        public static void SetMainFont(Font mainFont)
        {
            if (mainFont == null)
            {
                Log.Error("Main font is invalid.");
                return;
            }

            s_MainFont = mainFont;

            GameObject go = new GameObject();
            go.AddComponent<Text>().font = mainFont;
            Destroy(go);
        }

        protected override void OnInit(object userData)

        {
            base.OnInit(userData);

            m_CachedCanvas = gameObject.GetOrAddComponent<Canvas>();
            m_CachedCanvas.overrideSorting = true;
            OriginalDepth = m_CachedCanvas.sortingOrder;

            m_CanvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();

            RectTransform transform = GetComponent<RectTransform>();
            transform.anchorMin = Vector2.zero;
            transform.anchorMax = Vector2.one;
            transform.anchoredPosition = Vector2.zero;
            transform.sizeDelta = Vector2.zero;

            gameObject.GetOrAddComponent<GraphicRaycaster>();

            //TODO:在这里根据语言设置让界面显示本地化文本
            //Text[] texts = GetComponentsInChildren<Text>(true);
            //for (int i = 0; i < texts.Length; i++)
            //{
            //    texts[i].font = s_MainFont;
            //    if (!string.IsNullOrEmpty(texts[i].text))
            //    {
            //        texts[i].text = GameEntry.Localization.GetString(texts[i].text);
            //    }
            //}
        }

        protected override void OnOpen(object userData)

        {
            base.OnOpen(userData);
        }

        protected override void OnClose(bool isShutdown, object userData)

        {
            base.OnClose(isShutdown, userData);
        }

        protected override void OnPause()

        {
            m_CanvasGroup.blocksRaycasts = false;
        }

        protected override void OnResume()

        {
            m_CanvasGroup.blocksRaycasts = true;
        }

        protected override void OnCover()

        {
            base.OnCover();
        }

        protected override void OnReveal()

        {
            base.OnReveal();
        }

        protected override void OnRefocus(object userData)

        {
            base.OnRefocus(userData);
        }

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)

        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);
        }

        protected override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)

        {
            int oldDepth = Depth;
            base.OnDepthChanged(uiGroupDepth, depthInUIGroup);
            int deltaDepth = UGuiGroupHelper.DepthFactor * uiGroupDepth + DepthFactor * depthInUIGroup - oldDepth +
                             OriginalDepth;
            Canvas[] canvases = GetComponentsInChildren<Canvas>(true);
            for (int i = 0; i < canvases.Length; i++)
            {
                canvases[i].sortingOrder += deltaDepth;
            }
        }

        protected override void InternalSetVisible(bool visible)
        {
            gameObject.SetActive(visible);
            // if (visible)
            // {
            //     m_CanvasGroup.alpha = 1;
            //     m_CanvasGroup.blocksRaycasts = true;
            // }
            // else
            // {
            //     m_CanvasGroup.alpha = 0;
            //     m_CanvasGroup.blocksRaycasts = false;
            // }
        }
    }
}